package acm.pta11;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class 口罩发放 {

    static class Node implements Comparable<Node> {

        String name;
        String id;
        int order;
        boolean isIssue;
        int day = -10000000;
        int time;

        public Node(String name, String id, String str) {
            this.name = name;
            this.id = id;
//            this.isIssue = isIssue == 1;
            String[] date = str.split(":");
            time = Integer.parseInt(date[0]) * 60 + Integer.parseInt(date[1]);
        }

        @Override
        public int compareTo(Node o) {
            if (time == o.time) return order - o.order;
            return this.time - o.time;
        }

        public boolean check() {
            return id.length() == 18;
        }

        @Override
        public String toString() {
            return name + " " + id;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Node node = (Node) o;
            return Objects.equals(id, node.id);
        }

        @Override
        public int hashCode() {
            return Objects.hash(id);
        }
    }

    static LinkedHashMap<String, Node> map = new LinkedHashMap<>();   //记录上次获取记录
    static PriorityQueue<Node> queue = new PriorityQueue<>();

    public static Node get(String name,String id,int issue,String date) {
        if (id.length()!=18)return null;
        boolean isIssue = issue == 1;
        Node node = map.get(id);
        if (node == null){
            node = new Node(name, id, date);
            map.put(id, node);
        }
        node.isIssue=node.isIssue||isIssue;
        return node;
    }
    public static void main(String[] args) {
        Set<Node> set = new LinkedHashSet<>();
        Reader sc = new Reader();
        int n = sc.nextInt();
        int p = sc.nextInt();
        int day = 0;
        while (n-- > 0) {
            day++;
            queue.clear();
            int m = sc.nextInt();
            int k = sc.nextInt();
            for (int i = 0; i < m; i++) {
                Node curr=get(sc.next(), sc.next(), sc.nextInt(), sc.next());
                if (curr==null)continue;
                curr.order = i;
                if (curr.isIssue)
                    set.add(curr);
                if (curr.day<=day) {
                    queue.add(curr);
                    map.put(curr.id, curr);
                }
            }
            while (k-- > 0 && !queue.isEmpty()) {
                Node curr = queue.poll();
                curr.day = day+p+1;
                System.out.println(curr);
            }
        }
        set.forEach(System.out::println);
    }

    static
    class Reader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");

        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {    //内存中没有
                String nextLine = innerNextLine();
                if (nextLine == null) {     //且没有下一行
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);  //读入一行
            }
            return true;
        }

        public String nextLine() {
            tokenizer = new StringTokenizer("");    //清空内存中的字符
            return innerNextLine();
        }

        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }

}
